package com.xhwl.centiotdata.controller;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.dto.huawei.IVS3800NotificationDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IOrderCentService;
import com.xhwl.centiotdata.service.cent.IVehicleCentService;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordForInFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordForOutFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForInFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForOutFeignDTO;
import com.xhwl.common.query.cent.device.DeviceQuery;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/6/19 13:34
 * Description: 华为 IVS3800 摄像头智能识别事件订阅接口
 */
@Slf4j
@RestController
public class HuaweiSubscribeIVS3800Controller {

    @Autowired
    private IVehicleCentService vehicleCentService;

    @Autowired
    private IDeviceCentService deviceCentService;
    @Autowired
    private IOrderCentService orderCentService;

    @PostMapping("/subscription/observe")
    public String observe(@RequestBody JSONObject params) {
        IVS3800NotificationDTO dto = params.toJavaObject(IVS3800NotificationDTO.class);
        if (dto == null || CollectionUtils.isEmpty(dto.getData())) {
            return "Failure";
        }
        for (IVS3800NotificationDTO.IVS3800NotificationData item : dto.getData()) {
            if (item.getCommonInfo() != null && item.getCommonInfo().getObjectType() != null) {
                Integer objectType = item.getCommonInfo().getObjectType();
                if (objectType == 1 || objectType == 5) { // 机动车 或 人车混合
                    onDisposeVehicle(item);
                }
            }
        }
        return "Success";
    }

    /**
     * 处理车辆出入事件
     */
    private void onDisposeVehicle(IVS3800NotificationDTO.IVS3800NotificationData data) {
        if (StringUtils.isBlank(data.getCommonInfo().getCameraId()) || data.getPrivateInfo() == null) {
            return;
        }
        IVS3800NotificationDTO.IVS3800NotificationPrivateInfo info = data.getPrivateInfo();
        if (info.getMetaData() == null || info.getMetaData().getAttributes() == null) {
            return;
        }
        IVS3800NotificationDTO.IVS3800NotificationMetaAttributes attributes = info.getMetaData().getAttributes();
        if (StringUtils.isBlank(attributes.getPnr()) || "-1".equals(attributes.getPnr())) {
            return;
        }

        DeviceQuery query = new DeviceQuery();
        query.setIotDeviceId(data.getCommonInfo().getCameraCNCode());
        List<DeviceDTO> list = deviceCentService.list(null, query);

        if (!CollectionUtils.isEmpty(list)) {
            DeviceDTO deviceDTO = list.get(0);
            if (deviceDTO.getName().contains("出入口")) {
                onAddingVehicleInRecords(info, deviceDTO, data.getCommonInfo().getAlarmId());
            } else if (deviceDTO.getName().contains("入口")) {
                onAddingVehicleInRecords(info, deviceDTO, data.getCommonInfo().getAlarmId());
            } else if (deviceDTO.getName().contains("出口")) {
                onAddingVehicleOutRecord(info, deviceDTO, data.getCommonInfo().getAlarmId());
            }
        }
    }

    private void onAddingVehicleInRecords(IVS3800NotificationDTO.IVS3800NotificationPrivateInfo bo, DeviceDTO deviceDTO, String aid) {
        if (ChkUtil.notnull(bo.getMetaData().getAttributes().getDirec(), -1) == 3) {
            onAddingVehicleOutRecord(bo, deviceDTO, aid);
            return;
        }
        CreateVehicleAccessRecordForInFeignDTO accessRecord = new CreateVehicleAccessRecordForInFeignDTO();
        accessRecord.setProjectId(deviceDTO.getProjectId());
        accessRecord.setEnterTime(new Date());
        accessRecord.setEnterPictureUrl(bo.getPic() != null ? bo.getPic().getImageUrl() : "");
        accessRecord.setRecordId(aid);
        accessRecord.setEnterPassageName(deviceDTO.getName());
        accessRecord.setPlateNumber(bo.getMetaData().getAttributes().getPnr());

        try {
            orderCentService.onPushingReception(accessRecord, deviceDTO);
        } catch (Exception e) {
            log.error("请求推送车辆接待失败", e);
        }

        CreateVehicleAccessRecordsForInFeignDTO feignDTO = new CreateVehicleAccessRecordsForInFeignDTO();
        feignDTO.setIn(Collections.singletonList(accessRecord));
        vehicleCentService.addAccessRecordsForIn(feignDTO);
    }

    private void onAddingVehicleOutRecord(IVS3800NotificationDTO.IVS3800NotificationPrivateInfo bo, DeviceDTO deviceDTO, String aid) {
        if (ChkUtil.notnull(bo.getMetaData().getAttributes().getDirec(), -1) == 4) {
            onAddingVehicleInRecords(bo, deviceDTO, aid);
            return;
        }
        CreateVehicleAccessRecordForOutFeignDTO accessRecord = new CreateVehicleAccessRecordForOutFeignDTO();
        accessRecord.setProjectId(deviceDTO.getProjectId());
        accessRecord.setExitTime(new Date());
        accessRecord.setExitPictureUrl(bo.getPic() != null ? bo.getPic().getImageUrl() : "");
        accessRecord.setRecordId(aid);
        accessRecord.setExitPassageName(deviceDTO.getName());
        accessRecord.setPlateNumber(bo.getMetaData().getAttributes().getPnr());

        CreateVehicleAccessRecordsForOutFeignDTO feignDTO = new CreateVehicleAccessRecordsForOutFeignDTO();
        feignDTO.setOut(Collections.singletonList(accessRecord));
        vehicleCentService.addAccessRecordsForOut(feignDTO);
    }

}
